<%--
  Created by IntelliJ IDEA.
  User: CBWL
  Date: 2017/3/20
  Time: 17:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>组织机构</title>
    <link rel="stylesheet" href="assets/ztree/zTreeStyle/zTreeStyle.css" type="text/css">

    <script type="text/javascript" src="assets/ztree/jquery.ztree.core.min.js"></script>
    <script type="text/javascript" src="assets/ztree/jquery.ztree.excheck.min.js"></script>
    <script type="text/javascript" src="assets/ztree/jquery.ztree.exedit.min.js"></script>
</head>
<body>
<div ng-app="orgApp" ng-controller="OrgCtrl">
    <div class="col-md-3">
        <ui-tree url="system/organization/list" key="organizationTree" editable="true"></ui-tree>
    </div>
    <div class="col-md-9">
        <ui-form key="orgAdd" column="2">
            <div class="row">
                <input name="id" type="hidden" value="{{nodeData.id}}"/>
            </div>
            <ui-form-input name="code" label="机构编码:" value="{{nodeData.code}}"></ui-form-input>
            <ui-form-input name="parentCode" readonly="true" label="父编码:" value="{{nodeData.parentCode}}"></ui-form-input>
            <ui-form-input name="name" label="机构名称:" value="{{nodeData.name}}"></ui-form-input>
            <ui-form-input name="leader" label="站长工号:" value="{{nodeData.leader}}"></ui-form-input>
            <ui-form-input name="tel" label="联系电话:" value="{{nodeData.tel}}"></ui-form-input>
            <ui-form-select name="orgType" label="机构类型:" value="{{nodeData.orgType}}">
                <option value="0">仓库</option>
                <option value="1">站点</option>
                <option value="2">其他</option>
            </ui-form-select>
            <ui-form-select name="settlementType" label="结算类型:" value="{{nodeData.settlementType}}">
                <option value="0">投递员价</option>
                <option value="1">合作站点价</option>
                <option value="2">合作商价</option>
                <option value="3">大型合作商价</option>
            </ui-form-select>

            <ui-form-region label="地区"
                            p-name="provinceCode"
                            c-name="cityCode"
                            a-name="countyCode"
                            value={{nodeData.countyCode||nodeData.cityCode||nodeData.provinceCode}}
            ></ui-form-region>
            <ui-form-input name="address" label="详细地址:" value="{{nodeData.address}}"></ui-form-input>

            <ui-form-select name="settlementType" label="合作类型:" value="{{nodeData.settlementType}}">
                <%--0自营 1合作站 2合作商 3大型合作商--%>
                <option value="0">自营</option>
                <option value="1">合作站</option>
                <option value="2">合作商</option>
                <option value="3">大型合作商</option>
            </ui-form-select>
        </ui-form>
    </div>
    <div class="row" id="orgImport">
         <%--<a  class="btn btn-sm btn-primary" href="javascript:void(0);" ng-click="exportDatas()" target="_blank" >导出模板</a>--%>
         <%--<a  class="btn btn-sm btn-success" href="./html/organizationhelper.html" target="_blank" >导出帮助</a>--%>
         <%--<ui-upload url="system/organization/import" success="uploadSuccess"></ui-upload>--%>
    </div>
    <script>
        angular.module('orgApp',['ui.tree','ui.form']);
        angular.module('orgApp').controller('OrgCtrl', function($scope,eventHelper){
            $scope.uploadSuccess=function (returndata) {
                $("#orgImport").find(".upload-result").remove();
                if(returndata.data){
                    $("#orgImport").append('<a  class="btn btn-sm btn-success upload-result" href="system/organization/down?fileKey='+returndata.data+'" target="_blank" >下载导出结果</a>');
                }
                toastr.success("上传成功!");
            }
            $scope.exportDatas=function(){
                var params = {
                    url: 'system/organization/exporttemplate',
                    type: "POST"
                };
                var exportForm = $("<form>");
                $(exportForm).css("display", "none");
                $(exportForm).attr('target', 'targetFrame');
                $(exportForm).attr('method', params.type);
                $(exportForm).attr('action', params.url);
                exportForm = exportForm.appendTo($("body"));
                exportForm.submit();
                exportForm.remove();
            }

            var url="system/organization/add";
            $scope.doSubmit=function () {
                var a=$scope['$tree'].getCheckedNodes();
                $scope.nodeData=a[a.length-1];
            }
            $scope.editTree=function (treeId,node) {
                if(node.code==null){
                    toastr.error("根目录无法编辑")
                    return false;
                }
                $scope.nodeData=node;
                url="system/organization/update";
                $scope.$apply();
                eventHelper.downNotice($scope,'ngValueReset');
                return false;
            }
            $scope.addTree=function (treeId,node) {
                $scope.nodeData={parentCode:node.code};
                url="system/organization/add";
                $scope.$apply();
                eventHelper.downNotice($scope,'ngValueReset');
                return false;
            }
            $scope.removeTree=function (treeId,node) {
                if(node.code==null){
                    toastr.error("根目录无法删除")
                    return false;
                }
                $.ajax({
                    url:"system/organization/del",
                    type:"post",
                    data:{id:node.id}
                }).done(function (result) {
                    if(result.code=='0000'){
                        $scope['organizationTree']._queryData();
                        toastr.success("删除成功！");
                    }else{
                        toastr.error(result.msg);
                    }
                }).fail(function (err) {

                });
                return false;
            }

            $scope. nodeData={};
            $scope.$on('ui.form.submit',function (event,param) {
                $.ajax({
                    url:url,
                    type:"post",
                    data:param
                }).done(function (result) {
                    if(result.code=='0000'){
                        toastr.success("保存成功!");
                        $scope['organizationTree']._queryData();
                    }else{
                        toastr.error(result.msg);
                    }
                }).fail(function (err) {

                });
            })
        });
    </script>
</div>
</body>
</html>